package com.sharefang.aproject.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sharefang.aproject.R;
import com.sharefang.aproject.adapter.FragmentAdapter;
import com.sharefang.aproject.net.RxSubscribe;
import com.sharefang.aproject.net.ServiceFactory;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;

/**
 * Created by Administrator on 2016/8/12.
 */

@SuppressLint("ValidFragment")
public class SocietyFragment extends Fragment {
    private Context context;
    List<String> tags = new ArrayList<>();
    ViewPager viewPager;

    public ViewPager getViewPager() {
        return viewPager;
    }

    public SocietyFragment(Context context) {
        this.context = context;
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                Observable<List<String>> observable = ServiceFactory.INSTANCE.getFirstCategory();
                observable.subscribe(new RxSubscribe<List<String>>() {
                    @Override
                    protected void _onNext(List<String> strings) {
                        tags.clear();
                        tags.addAll(strings);
                    }

                    @Override
                    protected void _onError(String message) {

                    }
                });
            }
        });
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_viewpager, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        viewPager = (ViewPager) view.findViewById(R.id.vp_society);
        setupViewPager(viewPager);
    }
    void setupViewPager(ViewPager viewPager) {
        viewPager.setOffscreenPageLimit(3);
        FragmentAdapter fragmentAdapter = new FragmentAdapter(getChildFragmentManager());
        fragmentAdapter.addFragment(new HotFragment(context), "推荐");
        fragmentAdapter.addFragment(new ConcernFragment(context), "关注");
//        fragmentAdapter.addFragment(new ControlFragment(context), "我");

        viewPager.setAdapter(fragmentAdapter);
    }
}
